package com.ebring.shareEducation.controller;

import com.ebring.shareEducation.bean.Taskrelease;
import com.ebring.shareEducation.service.TaskreleaseService;
import com.ebring.shareEducation.service.UserVIPService;
import com.ebring.shareEducation.util.FileUtils;
import com.ebring.shareEducation.utils.RspMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
public class TestController {

    private final ResourceLoader resourceLoader;

    @Autowired
    public TestController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    @Autowired
    private UserVIPService userVIPService;

    @Value("${web.upload-path}")
    private String path;

    @Autowired
    private TaskreleaseService newTaskreleaseService;



    @GetMapping("/index")
    public String index(){
        return "home"; //当浏览器输入/index时，会返回 /templates/home.html页面
    }
    /**
     * 跳转到文件上传页面
     * @return
     */
    @RequestMapping("test")
    public String toUpload(){
        return "test";
    }

    /**
     *
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping("fileUpload")
    public String upload(@RequestParam("fileName") MultipartFile file, Map<String, Object> map){

        // 要上传的目标文件存放路径
        String localPath = "E:/Develop/Files/Photos";
        // 上传成功或者失败的提示
        String msg = "";
        String taskid="1";
        Taskrelease newTaskrelease=new Taskrelease();
        newTaskrelease.setTaskReleaseId(taskid);
        Map<String, Object> maps= (Map<String, Object>) newTaskreleaseService.selectUTaskrelease(newTaskrelease).getResult();
        List<Taskrelease> list= (List<Taskrelease>) maps.get("taskreleaselist");
        Taskrelease newTaskrelease1=list.get(0);
        newTaskrelease1.setTaskpath(localPath+"/"+file.getOriginalFilename());
        System.out.println(newTaskrelease.getTaskpath());
        RspMsg<Taskrelease> rspMsg =newTaskreleaseService.updateTaskrelease(newTaskrelease1);
        System.out.println(rspMsg.getMsg());
        if (FileUtils.upload(file, localPath, file.getOriginalFilename())){
            // 上传成功，给出页面提示
            msg = "上传成功！";
        }else {
            msg = "上传失败！";
        }
        // 显示图片
        map.put("msg", msg);
        map.put("fileName", file.getOriginalFilename());

        return "forward:/test";
    }

    /**
     * 显示单张图片
     * @return
     */
    @RequestMapping("show")
    public ResponseEntity showPhotos(String fileName){

        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/test/chargVIP")
    public void chargerVIP(String userId, String money, String vipLevel){
        int i = userVIPService.chargeVIP(userId, money, vipLevel);
        System.out.println(i);
    }

}
